package com.example.spring.bean.load08lifecycle.beanMethod;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 测试入口
 * <p> 看看初始化时机，单例和多例的区别
 * <p>
 */
public class LCTest1 {

    public static void main(String[] args) {

        //初始化容器的时候，就创建了term单例对象,并调用了方法
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(InitConfig.class);

        //第一次get term时候，直接获取，无初始化操作
        System.out.println("======get termObj...");
        TermObj term = context.getBean(TermObj.class);

        //第二次get term时候，直接获取，无初始化操作
        System.out.println("======get termObj2...");
        TermObj termObj = context.getBean(TermObj.class);

        //第一次get week时候，初始化week，并调用init
        System.out.println("======get weekObj...");
        WeekObj weekObj = context.getBean(WeekObj.class);

        //第二次get week时候，再次初始化week，并调用init
        //所以容器只负责初始化,但不会管理bean, 容器关闭不会调用销毁方法，而是把销毁交给了jvm
        System.out.println("======get weekObj...");
        WeekObj weekObj2 = context.getBean(WeekObj.class);


        System.out.println("======close");
        //关闭容器,调用term的destroy方法
        context.close();
    }

    /**
     * 输出结果
     * term constructor...
     * term init...
     * ======get termObj...
     * ======get termObj2...
     * ======get weekObj...
     * week constructor...
     * week init...
     * ======get weekObj...
     * week constructor...
     * week init...
     * ======close
     * term destroy...
     */
}
